﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _2011DBProject
{
    public partial class MakeGuild : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnMakeGuild.Attributes.Add("onClick", "return confirm('1억 NG를 공탁하게됩니다. \\n길드를 만드시겠습니까?');");

            
                

        }

        protected void btnMakeRoom_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet1 ds = new DataSet1();
                DataSet1TableAdapters.GUILDTableAdapter taGuild = new DataSet1TableAdapters.GUILDTableAdapter();
                taGuild.Connection.ConnectionString = Settings.strConnectionString;

                //Label templbl = (Label)(DataList1.Controls[0].Controls[2]).FindControl("GUILD_NAME");

                //Label tempNG = (Label)((DataList2.Controls[0].Controls[1]).
                //lblEnd.Text = tempNG.Text;
                Label tempNG = (Label)(DataListCharacter.Controls[0].Controls[16]).FindControl("NG_AMOUNTLabel");
                //bool templbl = DataList1.HasControls();

                DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGuildM = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                taGuildM.Connection.ConnectionString = Settings.strConnectionString;

                if(Convert.ToDecimal(taGuildM.ScalarQueryAnotherCG(Convert.ToDecimal(Session["SERVER"]), (string)Session["USER"])) == 0)
                {
                    if (taGuild.ScalarQueryGuildCheck(txtGuildName.Text, Convert.ToDecimal(Session["SERVER"])) == 0)
                    {
                        if (Convert.ToInt32(tempNG.Text) >= 100000000)
                        {
                            DataSet1.GUILDRow r = ds.GUILD.NewGUILDRow();
                            r.GUILD_MASTER_NAME = (string)Session["CHARACTER_NAME"];
                            r.SERVER_NO = Convert.ToDecimal(Session["SERVER"]);
                            r.GUILD_NAME = txtGuildName.Text;
                            r.GUILD_VMASTER_NAME = "0";
                            r.GUILD_POINT = 1;
                            r.GUILD_GRADE = 1;
                            r.GUILD_BENEFIT_NO = 1;
                            r.MEMBER_LIMIT = 30;
                            ds.GUILD.AddGUILDRow(r);
                            taGuild.Update(ds.GUILD);
                            Session.Add("GUILD_NAME", txtGuildName);

                            DataSet1TableAdapters.CHARACTERTableAdapter taCharacter = new DataSet1TableAdapters.CHARACTERTableAdapter();
                            taCharacter.Connection.ConnectionString = Settings.strConnectionString;
                            DataSet1.CHARACTERDataTable dtCharacter = taCharacter.GetDataByServerNoCharacterName(Settings.decServer, Settings.strCharacterName);
                            DataSet1.CHARACTERRow row = dtCharacter[0];

                            row.NG_AMOUNT -= 100000000;

                            taCharacter.Update(dtCharacter);

                           //DataSet1TableAdapters.GUILD_MEMBERTableAdapter taGM = new DataSet1TableAdapters.GUILD_MEMBERTableAdapter();
                            taGuildM.Connection.ConnectionString = Settings.strConnectionString;
                            DataSet1.GUILD_MEMBERRow gmr = ds.GUILD_MEMBER.NewGUILD_MEMBERRow();
                            gmr.CHARACTER_NAME = (string)Session["CHARACTER_NAME"];
                            gmr.SERVER_NO = Convert.ToDecimal(Session["SERVER"]);
                            gmr.MEMBER_GRADE = "마스터";
                            gmr.GUILD_NAME = txtGuildName.Text;
                            ds.GUILD_MEMBER.AddGUILD_MEMBERRow(gmr);
                            taGuildM.Update(ds.GUILD_MEMBER);

                            Session.Add("GUILD_NAME", txtGuildName.Text);
                            Settings.strGuildName = txtGuildName.Text;

                            GridView1.DataBind();
                            lblEnd.Visible = true;
                        }
                        else
                        {
                            string script = "<script>alert('엔지가 모자랍니다.');</script>";
                            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
                        }
                    }
                    else
                    {
                        string script = "<script>alert('서버에 같은 길드명이 있습니다.');</script>";
                        Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
                    }
                }
                else
                {
                    string script = "<script>alert('하나이상의 캐릭터가 다른 길드에 가입되어있습니다.');</script>";
                    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
                }
            }
            catch
            {
                string script = "<script>alert('에러번호 - #06 : 관리자에게 문의하세요.');</script>";
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "confirm", script);
            }

        }
        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Server.aspx");
        }
        protected void btnGoCharacter_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Character.aspx");
        }
        protected void btnGoChannel_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Channel.aspx");
        }
        protected void btnGoMain_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Gameroom.aspx");
        }
    }
}
